var loginStatus = '';

function modelInit() {
    //面包屑
    if (modelIndex.crumbs) {
        TV.structure(modelIndex.crumbs)
    }
    $.cookie('TV_CST_userName', '小王', {
        expires: 7
    });
    var indexList = new TV.CookieOpt().getOpt()
    // console.log(modelIndex.disHeader, 'modelIndex.disHeader')
    var index = indexList && typeof indexList.index == 'number' ? indexList.index : undefined;
    if (!modelIndex.disUser) {
        loginStatus = TV.UserMess()
    }
    right.toggleFocus(index);
}


/*
 * 1.右侧区
 */

cmsIndex.modules[cmsIndex.moduleGuid(cmsIndex.addModule())] = {
    modName: 'right',
    modNum: cmsIndex.moduleGuid(),
    map: {
        left: 0
    }
};

var right = cmsIndex.modules[cmsIndex.moduleGuid()].module = (function () {
    var mod = {
        modName: "index",
        modNum: cmsIndex.moduleGuid()
    },
        index = modelIndex.index, //位置
        listName = modelIndex.listName,  //模块名
        containerId = modelIndex.containerId; //包含模块的标签id

    //操作
    mod.toggleFocus = function (number) {
        var me = mod;
        if (number != undefined) {
            index = number;
        }
        TV.IndexOpt(containerId, listName, index);
        var elem = $("#" + containerId).children("." + listName);
        var elemTitle = $(".Curtitle");
        elemTitle.removeClass("title-active");
        for (var i = 0; i < elem.length; i++) {
            elem[i].className = listName;
        }
        if (index == -1) {
            elemTitle.addClass("title-active");
        } else if (elem.length) {
            elem[index].className = elem[index].className + ' active';
        }
        return me;
    };

    //index 位置调整
    mod.moveOp = function (directive) {
        var me = mod;
        index = direOpt(index, directive);
        mod.toggleFocus();
        return me;
    };
    mod.action = function (directive) {
        var me = mod;
        switch (directive) {
            case 'back':
                if (modelIndex.isVideo && mp) {
                    mp.stop();
                }
                if (modelIndex.isIndex) {
                    window.close();
                } else {
                    history.go(-1);

                }
                // history.go(-1);

                break;
            case 'enter':
                if (modelIndex.isVideo && mp) {
                    mp.stop();
                }
                if (isCache) {
                    var cookieTv = new TV.CookieOpt({
                        index: index
                    })
                    var indexList = cookieTv.setOpt();
                }
                if (index == -1) {
                    if (loginStatus) {
                        if (modelIndex.isIndex) {
                            window.location.href = './pages/user.html'
                        }
                        else {
                            window.location.href = '../pages/user.html'
                        }
                    } else {
                        if (modelIndex.isIndex) {
                            window.location.href = './pages/login.html'
                        } else {
                            window.location.href = '../pages/login.html'
                        }
                    }

                } else if (index >= 0) {

                    direEnter(index);

                }
                break;
        }
        return me;
    };
    mod.grabEvent = function (key) {
        var me = mod;
        var keyJson = TV.GetKey(key);
        if (keyJson.keyState && keyJson.keyState != 'enter' && keyJson.keyState != 'back') {
            me.moveOp(keyJson.keyState);
        } else {
            me.action(keyJson.keyState)
        }
        return keyJson.isBlock;


    };

    return mod;
})();

$(window).unload(function () {
    if (modelIndex.isVideo && mp) {
        mp.stop();
    }
});
cmsIndex.run();